package com.yuanqi.dz.dc.service.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import pnbclient.util.PnbLogFactory;
import pnblog.PnbLogging;

import com.yuanqi.dz.dc.util.DZConfigRead;
import com.yuanqitech.powermop.dz.DZDataInterface;
import com.yuanqitech.powermop.dz.DZInfo;
import com.yuanqitech.powermop.dz.DZInfoArray;
/**
 * 宜昌远安医院自助对账实现类
 * @author txk
 *
 */
public class YuanAnSelfMachinePosServiceImpl  implements DZDataInterface{
	//第一列：前6位是批次号，后6位是流水号；第二列 卡号；第三列交易时间；4：交易金额；5：交易类型；6：终端号
	private static int payflownoindex=0;
	public static  int cardnoindex=1;
	private  static int timeindex=2;
	private static int amountindex=3;
	private  static int trantypeindex=4;
	private static int termidindex=5;
	private static String charset="utf-8";
	private static int   start_row=0;

	private static  String splitchar=",";
	
	public static final PnbLogging LOG = PnbLogFactory.getLogger("PRE_DZ_LOG");

	public  DZInfoArray getDZData(String merchant_num,String dzdate){
		String  ya_local_path=DZConfigRead.getProperties("ya_local_path");
		String fileName = merchant_num + dzdate;
		File file = new File(ya_local_path,fileName);
		DZInfoArray dzInfoArray=new DZInfoArray();
		if(file.exists()){
			BufferedReader reader=null;
			List<DZInfo> list=new ArrayList<DZInfo>();
			try {
				reader =pnbclient.helper.StringHelper.isEmpty(charset) ? new BufferedReader(new FileReader(file))
				: new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset
						.forName(charset)));
				String lineText = null;
				int n=1;
				while ((lineText = reader.readLine()) != null) {
					if(n>=start_row){
						String[]  ss= splitLine(lineText,splitchar);
						if(null!=ss && ss.length>0){
							DZInfo dzinfo=new DZInfo();
							String payflowno=ss[payflownoindex];
							if(payflowno.length() > 6){
								//截取流水号
								payflowno = payflowno.substring(6, payflowno.length());
							}
							String cardno=ss[cardnoindex];
							String time=ss[timeindex];
							String amount=ss[amountindex];
							//去除对账文件里面的冗余金额为0.0 的脏数据
							if((Double.valueOf(amount.trim())) == 0d){
								continue;
							}
							String tran_type=ss[trantypeindex];
							String tremid=ss[termidindex];
							//唯一id:流水号+终端号
							dzinfo.setReferNum(payflowno);
							dzinfo.setPayFlowNO(payflowno);
							dzinfo.setTranCardNum(cardno);
							dzinfo.setTrDateTime(time);
							dzinfo.setAmount(Float.valueOf(amount));
							dzinfo.setTranCode(tran_type);
							dzinfo.setTermId(tremid);
							list.add(dzinfo);
						}
					}
					n++;
				}
				DZInfo[]  dzinfo=copylist(list);
				if(dzinfo.length==0){
					dzInfoArray.setRc("-1");
					dzInfoArray.setErrormsg("获取对账数据为空");
					dzInfoArray.setDzInfo(dzinfo);
				}else{
					dzInfoArray.setRc("1");
					dzInfoArray.setErrormsg("获取对账数据成功");
					dzInfoArray.setDzInfo(dzinfo);

				}

			}catch (Exception e) {
				LOG.addError("获取对账数据异常"+e);
				dzInfoArray.setRc("-1");
				dzInfoArray.setErrormsg("获取对账数据异常");
				dzInfoArray.setDzInfo(new DZInfo[0]);
				return dzInfoArray;
			}


		}else{
			dzInfoArray.setRc("-1");
			dzInfoArray.setErrormsg("对账文件不存在"+file.getName());
			dzInfoArray.setDzInfo(new DZInfo[0]);
		}
		return dzInfoArray;
	}

	public  String[] splitLine(String string, String splitchar) {
		if ("!".equals(splitchar))
			return StringUtils.splitPreserveAllTokens(string, '!');
		else if ("\\|".equals(splitchar))
			return StringUtils.splitPreserveAllTokens(string, '|');
		else
			return string.split(splitchar);
	}

	public DZInfo[]  copylist(List<DZInfo> list){
		if(null!=list  && list.size()>0){
			DZInfo[] dz=new DZInfo[list.size()];
			for (int i = 0; i < list.size(); i++) {
				dz[i]=list.get(i);
			}
			return dz;
		}else{
			DZInfo[] dz=new DZInfo[0];
			return dz;
		}

	}
	
	public  static void main(String[] args) {
		YuanAnSelfMachinePosServiceImpl y = new YuanAnSelfMachinePosServiceImpl();
		DZInfoArray d = y.getDZData("141525080620102","20170504");
	}
}

